package com.sgb.kjwl.view.ui.activity.house;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentLeaseLandListBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.sgb.kjwl.model.entity.lease.LeaseListEntity;
import com.sgb.kjwl.model.entity.request.LandFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.event.LeaseEvent;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.kjwl.view.ui.adapter.machine.LeaseListAdapter;
import com.sgb.kjwl.view.ui.adapter.machine.LeaseListTopAdapter;
import com.sgb.kjwl.view.widget.DictWindow5;
import com.sgb.kjwl.view.widget.ThreeLevelAreaWindow;
import com.sgb.kjwl.view.widget.lease.LandFliterPopWindow;
import com.sgb.kjwl.viewmodel.house.LeaseLandListFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerViewItemDecoration2;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 土地出租列表Fragment
 */
public class LeaseLandListFragment extends AppLazyFragment {
    @Inject
    LeaseLandListFragModel viewModel;
    private FragmentLeaseLandListBinding binding;
    private String categoryParentId = "";
    private int position;
    private DictWindow5 brandlWindow5;
    private List<DictEntity3> brandDictEntityList;
    private List<DictEntity3> moneyDictEntityList;
    private List<DictEntity3> ownershipDictEntityList;
    private List<DictEntity3> sortDictEntityList;
    //    private MultiLevelAreaPopWindow areaWindow;
    //    private TwoAreaPopWindow2 areaPopWindow;
    private ThreeLevelAreaWindow mAddressPop;
    private List<String> cityList;
    //    private List<BaseAreaEntity<List<BaseAreaEntity>>> areaEntityList;
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> areaEntityList;
    private LandFliterPopWindow popWindow;
    private List<DictEntity3> typeDictEntityList;
    private LeaseListTopAdapter topAdapter;//顶部的adapt
    private LeaseListAdapter adapter;
    private DictWindow5 classifyWindow;//分类
    private DictWindow5 sortWindow;
    private final boolean isFilterPickerVisible = true;
    private boolean isFirstCheck = false;//是否首次选中
    private int categoryFirst;
    private int categorySecond;
    private boolean isFilter = true;
    private String provinceName = "";
    private String cityName = "";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder()
                .appComponent(appComponent)
                .machineFragModule(new MachineFragModule(this))
                .build()
                .inject(this);
    }

    public static LeaseLandListFragment newInstance() {
        LeaseLandListFragment fragment = new LeaseLandListFragment();
        return fragment;
    }

    public static LeaseLandListFragment newInstance(String isSelf) {
        LeaseLandListFragment fragment = new LeaseLandListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("isSelf", isSelf);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_land_list, container, false);
        return binding.getRoot();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        checkDismiss(0);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onLazyLoad() {
        initExtra();
        init();
    }

    private void initExtra() {
        if (getArguments() != null) {
            String isSelf = getArguments().getString("isSelf");
            viewModel.filterJson.get().setIsSelf(isSelf);
            categoryFirst = getArguments().getInt("categoryFirst", 0);
            categorySecond = getArguments().getInt("categorySecond", 0);
        }
    }

    protected void initView() {
        binding.classifyLayout.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        binding.areaLayout.setOnClickListener(this);
        binding.sortLayout.setOnClickListener(this);
        binding.ivFilter.setOnClickListener(this);
        binding.ivMap.setOnClickListener(this);
        // 房屋列表 分类监听
//        binding.rvConditionSelection.setTabMode(TabLayout.MODE_SCROLLABLE);
//        binding.rvConditionSelection.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                checkDismiss(0);
//                if (isFirstCheck) {
//                    updateChooseTextStatus(binding.typeLayout, binding.tvType, 0, "类型");
//                }
//                isFirstCheck = true;
//                categoryParentId = typeDictEntityList.get(tab.getPosition()).getId();
//                viewModel.filterJson.get().setFirstCategoryNo(categoryParentId);
//                viewModel.filterJson.get().setSecondCategoryNo("");
//                viewModel.getAllList(false);
//                popWindow.setTypeDefault(classifyWindow.getCheckDefultPosition(categoryParentId), categoryParentId);
//                viewModel.getFilterModelModel(categoryParentId);
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
//
//        binding.tabType.setTabMode(TabLayout.MODE_SCROLLABLE);
//        binding.tabType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                checkDismiss(0);
//                String secondCategoryNo = brandDictEntityList.get(tab.getPosition()).getId();
//                viewModel.filterJson.get().setSecondCategoryNo(secondCategoryNo);
//                viewModel.getAllList(false);
//                popWindow.setBrandDefault(brandlWindow5.getCheckDefultPosition(secondCategoryNo), secondCategoryNo);
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });

        binding.selectLayout.setVisibility(isFilterPickerVisible ? View.VISIBLE : View.GONE);
        if (adapter == null) {
            adapter = new LeaseListAdapter(getActivity(), null);
        }
        adapter.setOnItemClick(new AdapterOnItemClick<LeaseListEntity>() {
            @Override
            public void onItemClick(LeaseListEntity entity, int position) {
                if (!ClickUtils.isDoubleClick()) {
                    viewModel.getH5Url(entity.getDataId());
                }
            }
        });
        binding.recyclerView.addItemDecoration(new RecyclerViewItemDecoration2(getResources().getDimension(R.dimen.dp10), getResources().getDimension(R.dimen.dp10)));
        binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        binding.recyclerView.setFootViewText("加载中", "我也是有底线的");
        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getAllList(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getAllList(true);
            }
        });
        if (isFilterPickerVisible) {
            initFilterView();
        }
    }

    private void initFilterView() {

        //分类选择
        classifyWindow = new DictWindow5(getActivity());
        classifyWindow.setFocusable(false);
        classifyWindow.setOutsideTouchable(false);
        classifyWindow.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                viewModel.filterJson.get().setFirstCategoryNo(dictEntity.getId());
                viewModel.getAllList(false);
                popWindow.setTypeDefault(classifyWindow.getCheckDefultPosition(dictEntity.getId()), dictEntity.getId());
            }
        });
        classifyWindow.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                binding.classifyArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
            }
        });

        //类型选择
        brandlWindow5 = new DictWindow5(getActivity());
        brandlWindow5.setFocusable(false);
        brandlWindow5.setOutsideTouchable(false);
        brandlWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                updateChooseTextStatus(binding.typeLayout, binding.tvType, 2, dictEntity.getName());
                viewModel.filterJson.get().setSecondCategoryNo(dictEntity.getId());
                viewModel.getAllList(false);
                popWindow.setBrandDefault(brandlWindow5.getCheckDefultPosition(dictEntity.getId()), dictEntity.getId());
            }
        });
        brandlWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                updateChooseTextStatus(binding.typeLayout, binding.tvType, "类型".equals(binding.tvType.getText()) ? 0 : 2, null);
            }
        });
//        //面积选择
//        moneyWindow5 = new DictWindow5(getActivity());
//        moneyWindow5.setFocusable(false);
//        moneyWindow5.setOutsideTouchable(false);
//        moneyWindow5.setOnCallBack(new DictWindow5.OnCallBack() {
//            @Override
//            public void result(DictEntity3 dictEntity) {
//                viewModel.filterJson.get().setRentalArea(dictEntity.getId());
//                viewModel.getAllList(false);
//                popWindow.setMoneyDefault(moneyWindow5.getCheckDefultPosition(dictEntity.getId()), dictEntity.getId());
//            }
//        });
//        moneyWindow5.setOnCloseBack(new DictWindow5.OnCloseBack() {
//            @Override
//            public void dismiss() {
//                binding.moneyArrowView.setImageResource(R.mipmap.ic_down_arrow_n);
//            }
//        });

        //排序选择
        sortWindow = new DictWindow5(getActivity());
        sortWindow.setFocusable(true);
        sortWindow.setOutsideTouchable(true);
        sortWindow.setOnCallBack(new DictWindow5.OnCallBack() {
            @Override
            public void result(DictEntity3 dictEntity) {
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, 2, dictEntity.getName());
                viewModel.filterJson.get().setSortWay(dictEntity.getId());
                viewModel.getAllList(false);
                popWindow.setSortDefault(sortWindow.getCheckDefultPosition(dictEntity.getId()), dictEntity.getId());
            }
        });
        sortWindow.setOnCloseBack(new DictWindow5.OnCloseBack() {
            @Override
            public void dismiss() {
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, "排序".equals(binding.tvSort.getText()) ? 0 : 2, null);
            }
        });

        mAddressPop = new ThreeLevelAreaWindow(getActivity());
        mAddressPop.setBgView(binding.grayLayout);
        mAddressPop.setMemberHeight();
        mAddressPop.setProvinceSingle();
        mAddressPop.setCitySingle();
        mAddressPop.setAreaMore();
        mAddressPop.setShowSimpleName(true);
        mAddressPop.setOnResultClick((key1, key2, key3) -> {
            try {
                List<BaseAreaEntity> listKey1 = (List<BaseAreaEntity>) key1;
                List<BaseAreaEntity> listKey2 = (List<BaseAreaEntity>) key2;
                List<BaseAreaEntity> listKey3 = (List<BaseAreaEntity>) key3;
                viewModel.filterJson.get().setAreaNos(getCityList(listKey3));
                if (listKey2 != null && listKey2.size() > 0) {
                    cityName = listKey2.get(0).getName();
                    viewModel.filterJson.get().setCityNo(listKey2.get(0).getNo());
                } else {
                    viewModel.filterJson.get().setCityNo("");
                }
                provinceName = listKey1.get(0).getName();
                viewModel.filterJson.get().setProvinceNo(listKey1.get(0).getNo());
                if (!TextUtils.isEmpty(cityName) && "不限".equals(cityName)) {
                    updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, cityName);
                } else {
                    updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, provinceName);
                }
            } catch (Exception exception) {
            }

            viewModel.getAllList(false);
        });
        mAddressPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? 0 : 2, null);
            }
        });

//        areaWindow = new MultiLevelAreaPopWindow(getActivity());
//        areaWindow.setBgView(binding.grayLayout);
//        areaWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
//            @Override
//            public void result(Object key1, Object key2, Object key3) {
//                List<BaseAreaEntity> selectedEntities = (List<BaseAreaEntity>) key3;
//                cityList.clear();
//                if (selectedEntities != null) {
//                    for (BaseAreaEntity selected : selectedEntities) {
//                        cityList.add(selected.getNo());
//                    }
//                }
//                viewModel.filterJson.get().setCityNo(((BaseAreaEntity) key2).getNo());
//                viewModel.filterJson.get().setAreaNos(cityList);
//                viewModel.getAllList(false);
//                updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, ((BaseAreaEntity) key2).getName());
//            }
//        });
//        areaWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? 0 : 2, null);
//            }
//        });

//        //地区选择
//        areaPopWindow = new TwoAreaPopWindow2(getActivity());
//        areaPopWindow.setFocusable(false);
//        areaPopWindow.setOutsideTouchable(false);
//        areaPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
//            @Override
//            public void result(Object key1, Object key2, Object key3) {
//                updateChooseTextStatus(binding.areaLayout, binding.tvArea, 2, ((ParentAreaEntity) key1).getName());
//                List<ParentAreaEntity> selectedEntities = (List<ParentAreaEntity>) key2;
//                cityList.clear();
//                if (selectedEntities != null) {
//                    for (ParentAreaEntity selected : selectedEntities) {
//                        cityList.add(selected.getNo());
//                    }
//                }
//                viewModel.filterJson.get().setCityNo(((ParentAreaEntity)key1).getNo());
//                viewModel.filterJson.get().setAreaNos(cityList);
//                viewModel.getAllList(false);
//            }
//        });
//        areaPopWindow.setOnCloseBack(new TwoAreaPopWindow2.OnCloseBack() {
//            @Override
//            public void dismiss() {
//                updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? 0 : 2, null);
//            }
//        });

        //筛选界面
        popWindow = new LandFliterPopWindow(getActivity());
//        popWindow.setBgView(binding.grayLayout);
        popWindow.setFilterResultClick(new LandFliterPopWindow.FilterResultClick() {
            @Override
            public void result(LandFilterJson filterJson, List<DictEntity3> data, SparseBooleanArray checkBooleanArray) {
                if (TextUtils.isEmpty(filterJson.getSecondCategoryName())) {
                    updateChooseTextStatus(binding.typeLayout, binding.tvType, 2, "不限");
                } else {
                    updateChooseTextStatus(binding.typeLayout, binding.tvType, 2, filterJson.getSecondCategoryName());
                }
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, 2, filterJson.getSortName());
                viewModel.filterJson.get().setFirstCategoryNo(filterJson.getFirstCategoryNo());
                viewModel.filterJson.get().setSecondCategoryNo(filterJson.getSecondCategoryNo());
                viewModel.filterJson.get().setRentalArea(filterJson.getRentalArea());
                viewModel.filterJson.get().setOwnership(filterJson.getOwnership());
                viewModel.filterJson.get().setSortWay(filterJson.getSortWay());

                viewModel.getAllList(false);
//                binding.rvConditionSelection.getTabAt(classifyWindow.getCheckDefultPosition(filterJson.getFirstCategoryNo())).select();
//                leaseHouseListActivity.changeType(getCheckTypeDefultPosition(machineFilterJson3.getCarModelId()));
            }

            @Override
            public void queryTypeChild(String id) {
                viewModel.getFilterModelModel(id);
            }

            @Override
            public void queryNameChild(String id) {
            }
        });
    }

    private void init() {
        viewModel.filterJson.get().setProvinceNo(viewModel.getAreaCodeModel());
        cityList = new ArrayList<>();
        viewModel.getFilterTypeModel("5");
        viewModel.getFilterSort();
        viewModel.getAllList(false);
    }

    public void showListFailure() {
        binding.recyclerView.loadMoreComplete();
        binding.recyclerView.refreshComplete();
    }

    public void showList(List<LeaseListEntity> list, boolean isLoadMore) {
        binding.recyclerView.setNoMore(list.size() < viewModel.pageSize);
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() != 0) {
                adapter.addDatas(list);
            }
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }

            binding.recyclerView.refreshComplete();
            adapter.setDatas(list);
        }
    }

    public void showTypePopWindow(List<DictEntity3> data) {
        this.typeDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        if ("不限".equals(data.get(0).getName().trim())) {
            typeDictEntityList.get(0).setName("全部");
            typeDictEntityList.remove(0);
        }

        int spanCount = 5;
        if (data.size() > 10) {
            spanCount = data.size() / 2;
            if (data.size() % 2 > 0) {
                spanCount += 1;
            }
        }
        topAdapter = new LeaseListTopAdapter(getActivity());
        binding.rvTop.setLayoutManager(new GridLayoutManager(getActivity(), spanCount));
        binding.rvTop.setAdapter(topAdapter);
        topAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 bean, int position) {
                LeaseCategoryListActivity.start(getActivity(), "5", bean.getId(), bean.getName());
            }
        });
        topAdapter.clearDatas();
        topAdapter.addDatas(typeDictEntityList);
    }

//    //设置一级分类数据
//    public void showTypePopWindow(List<DictEntity3> data) {
//        this.typeDictEntityList = data;
//        if (data == null || data.size() == 0) {
//            return;
//        }
//
//        if ("不限".equals(data.get(0).getName().trim())) {
//            typeDictEntityList.get(0).setName("全部");
//        }
//
//        categoryParentId = viewModel.filterJson.get().getFirstCategoryNo();
//        if (TextUtils.isEmpty(categoryParentId)) {
//            categoryParentId = data.get(0).getId();
//        }
//        classifyWindow.setData(data);
//        classifyWindow.setCheckDefaultById(categoryParentId);
////        popWindow.setTypeData(data);
////        popWindow.setTypeDefault(classifyWindow.getCheckDefultPosition(categoryParentId), categoryParentId);
//        viewModel.filterJson.get().setFirstCategoryNo(categoryParentId);
//        for (int i = 0; i < typeDictEntityList.size(); i++) {
//            binding.rvConditionSelection.addTab(binding.rvConditionSelection.newTab().setText(typeDictEntityList.get(i).getName()));
//        }
//        binding.rvConditionSelection.selectTab(binding.rvConditionSelection.getTabAt(categoryFirst));
//    }

    //设置二级分类数据
    public void showBrandPopWindow(List<DictEntity3> data) {
//        this.brandDictEntityList = data;
//        if (data == null || data.size() == 0 || !isFilter) {
//            binding.tabType.setVisibility(View.GONE);
//            binding.viewTypeTabLine.setVisibility(View.GONE);
//            return;
//        } else {
//            binding.tabType.setVisibility(View.VISIBLE);
//            binding.viewTypeTabLine.setVisibility(View.VISIBLE);
//        }
//
//        brandlWindow5.setData(data);
//        popWindow.setBrandData(data);
//        String defaultId = viewModel.filterJson.get().getSecondCategoryNo();
//        if (TextUtils.isEmpty(defaultId)) {
//            defaultId = data.get(0).getId();
//        }
//        brandlWindow5.setCheckDefaultById(defaultId);
////        int position = brandlWindow5.getCheckDefultPosition(defaultId);
////        popWindow.setBrandDefault(position < 0 ? 0 : position, defaultId);
////        viewModel.filterJson.get().setSecondCategoryNo(defaultId);
//
//        //设置类型tab
//        if ("不限".equals(data.get(0).getName().trim())) {
//            data.get(0).setName("全部");
//            if (data.size() == 1) {
//                binding.tabType.setVisibility(View.GONE);
//                binding.viewTypeTabLine.setVisibility(View.GONE);
//                return;
//            } else {
//                binding.tabType.setVisibility(View.VISIBLE);
//                binding.viewTypeTabLine.setVisibility(View.VISIBLE);
//            }
//        }
//
//        binding.tabType.removeAllTabs();
//        for (int i = 0; i < data.size(); i++) {
//            binding.tabType.addTab(binding.tabType.newTab().setText(data.get(i).getName()));
//        }
//        binding.tabType.selectTab(binding.tabType.getTabAt(0));
    }

    //设置面积数据
    public void showRentalAreaPopWindow(List<DictEntity3> data) {
        this.moneyDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }

        String defaultId = viewModel.filterJson.get().getRentalArea();
        if (TextUtils.isEmpty(defaultId)) {
            defaultId = data.get(0).getId();
        }
        popWindow.setMoneyData(data);
        popWindow.setMoneyDefault(0, defaultId);
        viewModel.filterJson.get().setRentalArea(defaultId);
    }

    //设置土地所有权数据
    public void showOwnershipPopWindow(List<DictEntity3> data) {
        this.ownershipDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }

        String defaultId = viewModel.filterJson.get().getOwnership();
        if (TextUtils.isEmpty(defaultId)) {
            defaultId = data.get(0).getId();
        }
        popWindow.setOwnershipData(data);
        popWindow.setOwnershipDefault(0, defaultId);
        viewModel.filterJson.get().setOwnership(defaultId);
    }

    //设置排序数据
    public void showSortPopWindow(List<DictEntity3> data) {
        this.sortDictEntityList = data;
        if (data == null || data.size() == 0) {
            return;
        }
        String defaultId = viewModel.filterJson.get().getSortWay();
        if (TextUtils.isEmpty(defaultId)) {
            defaultId = data.get(0).getId();
        }
        sortWindow.setData(data);
        sortWindow.setCheckDefaultById(defaultId);
        popWindow.setSortData(data);
        popWindow.setSortDefault(0, defaultId);
        viewModel.filterJson.get().setSortWay(defaultId);
    }

//    public void showAreaPop(List<BaseAreaEntity<List<BaseAreaEntity>>> datas) {
//        this.areaEntityList = datas;
//        if (datas == null || datas.size() == 0) return;
//        BaseAreaEntity baseAreaEntity = new BaseAreaEntity();
//        baseAreaEntity.setName("不限");
//        baseAreaEntity.setNo("");
//        baseAreaEntity.setChildren(new ArrayList<>());
//        datas.add(0, baseAreaEntity);
//        for (int i = 0; i < datas.size(); i++) {
//            datas.get(i).getChildren().add(0, baseAreaEntity);
//        }
//        areaWindow.setCityList(datas);
//        areaWindow.setCitySelectType(true);
//        areaWindow.setAreaSelectType(true, false);
//    }

    public void showAddressDatas(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data, String provinceNo) {
        this.areaEntityList = data;
        if (data != null && data.size() > 0) {
            viewModel.filterJson.get().setProvinceNo(provinceNo);
            mAddressPop.setData(data, true, provinceNo);
        }
    }

    private List<String> getCityList(List<BaseAreaEntity> list) {
        cityList = new ArrayList<>();
        for (BaseAreaEntity entity : list) {
            if (!TextUtils.isEmpty(entity.getNo())) {
                cityList.add(entity.getNo());
            }
        }
        return cityList;
    }

    @Override
    public void onClick(View v) {
        checkDismiss(v.getId());
        switch (v.getId()) {
            case R.id.classify_layout://分类
                binding.classifyArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
                classifyWindow.setCheckDefaultById(viewModel.filterJson.get().getFirstCategoryNo());
                classifyWindow.showPopwindow(binding.classifyLayout);
                if (typeDictEntityList == null) {
                    viewModel.getFilterTypeModel("5");
                }
                break;
            case R.id.type_layout://类型
                updateChooseTextStatus(binding.typeLayout, binding.tvType, "类型".equals(binding.tvType.getText()) ? -1 : 1, null);
                brandlWindow5.setCheckDefaultById(viewModel.filterJson.get().getSecondCategoryNo());
                brandlWindow5.showPopwindow(binding.typeLayout);
                viewModel.getFilterModelModel(viewModel.filterJson.get().getFirstCategoryNo());
                break;
            case R.id.area_layout://地区
                updateChooseTextStatus(binding.areaLayout, binding.tvArea, "地区".equals(binding.tvArea.getText()) ? -1 : 1, null);
                mAddressPop.showPopwindow(binding.areaLayout);
                if (areaEntityList == null) {
                    viewModel.getCityData();
                }
                break;
//            case R.id.money_layout://面积
//                binding.moneyArrowView.setImageResource(R.mipmap.ic_up_arrow_n);
//                moneyWindow5.setCheckDefaultById(viewModel.filterJson.get().getRentalArea());
//                moneyWindow5.showPopwindow(binding.moneyLayout);
//                if (moneyDictEntityList == null) {
//                    viewModel.getAreaNumModel();
//                }
//                break;
            case R.id.sort_layout://排序
                updateChooseTextStatus(binding.sortLayout, binding.tvSort, "排序".equals(binding.tvSort.getText()) ? -1 : 1, null);
                sortWindow.setCheckDefaultById(viewModel.filterJson.get().getSortWay());
                sortWindow.showPopwindow(binding.sortLayout);
                if (sortDictEntityList == null) {
                    viewModel.getFilterSort();
                }
                break;
            case R.id.iv_filter://筛选
                if (typeDictEntityList == null) {
                    viewModel.getFilterTypeModel("5");
                }
//                if (brandDictEntityList == null) {
//                viewModel.getFilterModelModel(categoryParentId);
//                }
                if (moneyDictEntityList == null) {
                    viewModel.getAreaNumModel();
                }
                if (ownershipDictEntityList == null) {
                    viewModel.getOwnershipModel();
                }
                if (sortDictEntityList == null) {
                    viewModel.getFilterSort();
                }
//                popWindow.showPopwindow(binding.filterLayout);
                popWindow.showPopwindowRight(getActivity().getWindow().getDecorView());
                popWindow.backgroundAlpha(0.5f);
                break;
            case R.id.iv_map:
                startMap();
                break;
        }
    }

    private void checkDismiss(int resId) {
        if (classifyWindow != null && classifyWindow.isShowing() && R.id.classify_layout != resId) {
            classifyWindow.dismiss();
        }
        if (brandlWindow5 != null && brandlWindow5.isShowing() && R.id.type_layout != resId) {
            brandlWindow5.dismiss();
        }
        if (mAddressPop != null && mAddressPop.isShowing() && R.id.area_layout != resId) {
            mAddressPop.dismiss();
        }
        if (sortWindow != null && sortWindow.isShowing() && R.id.sort_layout != resId) {
            sortWindow.dismiss();
        }
    }

    //更新选项颜色
    private void updateChooseTextStatus(View view, TextView textView, int value, String name) {
        if (value == 0) {
            setTextViewBg(view, textView, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
        } else if (value == -1) {
            setTextViewBg(view, textView, R.drawable.search_bg, R.mipmap.ic_up_arrow_n, R.color.color_333333);
        } else if (value == 1) {
            setTextViewBg(view, textView, R.drawable.search_new_blue_bg, R.mipmap.icon_up_red, R.color.color_main);
        } else if (value == 2) {
            setTextViewBg(view, textView, R.drawable.search_new_blue_bg, R.mipmap.icon_down_red, R.color.color_main);
        }
        if (TextUtils.isEmpty(name)) return;
        textView.setText(name);
    }

    //设置背景色和图标
    private void setTextViewBg(View view, TextView textView, int bg, int img, int textColor) {
        view.setBackgroundResource(bg);
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable_n, null);
        textView.setTextColor(getResources().getColor(textColor));
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(LeaseEvent event) {//搜索
        if (event.getMessageTag() == EventConstant.EVENT_HOUSE_FILTER) {
            String text = (String) event.getMessageData();
            viewModel.filterJson.get().setTitle(text);
            viewModel.getAllList(false);
        }else if (event.getMessageTag() == EventConstant.EVENT_LEASE_FILTER){//定位坐标改变后刷新
            viewModel.getAllList(false);
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public void startMap() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), SelectAddressActivity.class);
        intent.putExtra(SelectAddressActivity.TITLE, "详细地址");
        intent.putExtra(SelectAddressActivity.HINT, "请输入地址");
        intent.putExtra(SelectAddressActivity.SAVE_TEXT, "确定");
        if(!TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData("distance_latitude",""))){
            intent.putExtra(SelectAddressActivity.LATITUDE, Double.parseDouble(SharedPreferenceUtil.getInstance().getSaveStringData("distance_latitude","")));
            intent.putExtra(SelectAddressActivity.LONGTITUDE, Double.parseDouble(SharedPreferenceUtil.getInstance().getSaveStringData("distance_longitude","")));
        }
        startActivityForResult(intent, 255);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 255 && data != null) {
                double lat = data.getDoubleExtra(SelectAddressActivity.LATITUDE, 0);
                double lon = data.getDoubleExtra(SelectAddressActivity.LONGTITUDE, 0);
                String address = data.getStringExtra(SelectAddressActivity.CURRENT_POSITION);
                //保存定位数据,用来给后端传参计算距离
                SharedPreferenceUtil.getInstance().saveData("distance_latitude", String.valueOf(lat));
                SharedPreferenceUtil.getInstance().saveData("distance_longitude", String.valueOf(lon));
                EventBus.getDefault().postSticky(LeaseEvent.make().setMessageTag(EventConstant.EVENT_LEASE_FILTER));
            }
        }
    }
}
